/**
 * 拜访记录管理初始化
 */
var Visit = {
    id: "VisitTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Visit.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '权限', field: 'editable', visible: false, align: 'center', valign: 'middle'},
        {title: '企业名称', field: 'customerCompanyName', visible: true, align: 'center', valign: 'middle'},
        {title: '拜访日', field: 'visitDate', visible: true, align: 'center', valign: 'middle'},
        {title: '开始时间', field: 'beginTime', visible: true, align: 'center', valign: 'middle'},
        {title: '结束时间', field: 'endTime', visible: true, align: 'center', valign: 'middle'},
        {title: '地点', field: 'place', visible: true, align: 'center', valign: 'middle'},
        {title: '形式', field: 'form', visible: true, align: 'center', valign: 'middle'},
        {title: '乙方人员', field: 'visitorName', visible: true, align: 'center', valign: 'middle'},
        {title: '随访人员', field: 'entourage', visible: true, align: 'center', valign: 'middle'},
        {title: '拜访目的', field: 'target', visible: true, align: 'center', valign: 'middle'},
        {title: '拜访对象', field: 'targetPerson', visible: true, align: 'center', valign: 'middle'},
        {title: '拜访纪要', field: 'summary', visible: true, align: 'center', valign: 'middle'},
        {title: '下一步措施', field: 'nextStep', visible: true, align: 'center', valign: 'middle'},
        {title: '主要机会和存在问题', field: 'challengesAndOpportunities', visible: true, align: 'center', valign: 'middle'},
        {title: '产品反馈', field: 'feedbackV', visible: true, align: 'center', valign: 'middle'},
        {
            title: '操作',
            field: "",
            visible: true,
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return '<button class="btn btn-info btn-sm rightSize detailBtn" type="button" onclick="Visit.showPic('+row.id+')"><i class=""></i>更多</button>';
            }
        }
    ];
};

/**
 * 检查是否选中
 */
Visit.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Visit.seItem = selected[0];
        return true;
    }
};

Visit.checkEditPermission = function () {
    if (this.check()) {
        if (Visit.seItem.editable === 1) {
            return true
        }else{
            Feng.info("您没有该操作权限！");
        }
    }
};

/**
 * 点击添加拜访记录
 */
Visit.openAddVisit = function () {
    var index = layer.open({
        type: 2,
        title: '添加拜访记录',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/visit/visit_add'
    });
    this.layerIndex = index;
    layer.full(index);
};

/**
 * 打开查看拜访记录详情
 */
Visit.openVisitDetail = function () {
    if (this.checkEditPermission()) {
        var index = layer.open({
            type: 2,
            title: '拜访记录详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/visit/visit_update/' + Visit.seItem.id
        });
        this.layerIndex = index;
        layer.full(index);
    }
};

/**
 * 展示图片
 */
Visit.showPic = function (id) {
    var index = layer.open({
        type: 2,
        title: '图片列表',
        area: ['800px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/visit/visit_pic_show/' + id
    });
    this.layerIndex = index;
};



/**
 * 删除拜访记录
 */
Visit.delete = function () {
    if (this.check()) {
        var operation = function () {
        var ajax = new $ax(Feng.ctxPath + "/visit/delete", function (data) {
            Feng.success("删除成功!");
            Visit.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("visitId", Visit.seItem.id);
        ajax.start();
    }
    Feng.confirm("是否删除?", operation);
}
};

Visit.resetSearch = function(){
    $("#begin_visitDate").val("");
    $("#end_visitDate").val("");
    $("#key").val("");
    $("#feedback").val("");
    Visit.search();
}
/**
 * 查询拜访记录列表
 */
Visit.search = function () {
    var queryData = {};
    queryData['begin_visitDate'] = $("#begin_visitDate").val();
    queryData['end_visitDate'] = $("#end_visitDate").val();
    queryData['key'] = $("#key").val();
    queryData['feedback'] = $("#feedback").val();
    Visit.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Visit.initColumn();
    var table = new BSTable(Visit.id, "/visit/list", defaultColunms);
    table.setPaginationType("server");
    Visit.table = table.init();

    //执行一个laydate实例
    laydate.render({
        elem: '#begin_visitDate' //指定元素
    });

    laydate.render({
        elem: '#end_visitDate' //指定元素
    });

});
